package com.ubxtech.core.dto;

import lombok.extern.slf4j.Slf4j;

import java.util.Comparator;
import java.util.Optional;

/**
 * @author Victor.Xiao
 * @since 2023-10-12 16:19
 **/
@Slf4j
public class TreeNodeComparator<T> implements Comparator<TreeNode<T>> {
    @Override
    public int compare(TreeNode o1, TreeNode o2) {
        //这里测试都不会为空
        if (o1 == null || o2 == null){
            return 0;
        }
        int order1 = Optional.ofNullable(o1.getOrderNo()).orElse(Integer.MAX_VALUE);
        int order2 = Optional.ofNullable(o2.getOrderNo()).orElse(Integer.MAX_VALUE);

        int result = order1-order2;
        if (result == 0) {
            //相等treeSet会add节点失败
            if (o1.equals(o2) && o1.hashCode() == o2.hashCode()) {
                return 0;
            } else {
                return 1;
            }
        }
        return result;
    }
}
